package cn.knet.excel.imp.knetexceltool.service;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;
import java.util.concurrent.atomic.LongAdder;

/**
 * 读取txt文件中的网址，逐行删除bind中的历史数据
 */
@Slf4j
public class BindCleanThread implements Runnable {

    private DnsService dnsService;

    private LongAdder global_okAdder = new LongAdder();
    private LongAdder global_failAdder = new LongAdder();

    private File wzFile;

    public BindCleanThread(File wzFile, LongAdder global_okAdder, LongAdder global_failAdder,DnsService dnsService) {
        this.global_okAdder = global_okAdder;
        this.global_failAdder = global_failAdder;
        this.wzFile=wzFile;
        this.dnsService=dnsService;
    }


    @Override
    public void run() {
        try {
            List<String> wzList= FileUtils.readLines(wzFile, Charset.defaultCharset());
            for(String domain:wzList){
                try {
                    dnsService.remove("@",domain,"网址");
                    dnsService.remove("www",domain,"网址");
                    dnsService.remove("*",domain,"网址");
                    global_okAdder.increment();
                } catch (Exception e) {
                    e.printStackTrace();
                    log.error("when deleting wz domain[{}] from bind,error occr !",domain);
                    global_failAdder.increment();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
